import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by WheelChen on 2017/5/23.
 */
public class Shape implements GLEventListener {
    private int type;
    private float rtri = 0.0f;          //旋转角度
    private float move = 0.0f;          //平移距离
    private boolean isRotate = false;   //是否旋转
    private boolean isMove = false;     //是否平移
    private boolean isScale = false;    //是否缩放
    private int direction = 1;

    public float getRtri() {
        return rtri;
    }

    public void setRtri(float rtri) {
        this.rtri = rtri;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public void init(GLAutoDrawable glAutoDrawable) {

    }

    @Override
    public void dispose(GLAutoDrawable glAutoDrawable) {

    }

    @Override
    public void display(GLAutoDrawable glAutoDrawable) {
        final GL2 gl2 = glAutoDrawable.getGL().getGL2();
        //清理深度缓存
        gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

        if (isScale) {
            gl2.glScalef(0.50f,0.25f,0.50f );
            isScale = false;
        }

        gl2.glRotatef( rtri, 0.0f, 1.0f, 0.0f );//triangle rotation

        switch (this.type){
            case 1:Line(gl2);break;
            case 2:Triangle(gl2);break;
            case 3:Quad(gl2);break;
        }

        gl2.glFlush();
        if (isRotate) rtri += 0.01f;  //assigning the angle
        if (isMove) {
            move += direction * 0.01f;

            if (move >= 0.2f) {
                direction = -1;
            }
            if (move <= -0.2f){
                direction = 1;
            }
        }
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        final GL2 gl = drawable.getGL().getGL2();
        //防止被零除
        if (height <= 0) // avoid a divide by zero error!
            height = 1;
        final float h = (float) width / (float) height;
        //设置视窗的大小
        gl.glViewport(0, 0, width, height);
        //选择投影矩阵 ,投影矩阵负责为我们的场景增加透视。
        gl.glMatrixMode(GL2.GL_PROJECTION);
        //重置投影矩阵;
        gl.glLoadIdentity();

        //设置视口的大小
//        glu.gluPerspective(45.0f, h, 1.0, 20.0);
        //启用模型观察矩阵；模型观察矩阵中存放了我们的物体讯息。
        gl.glMatrixMode(GL2.GL_MODELVIEW);

        gl.glLoadIdentity();
    }
    /**
     *注册事件
     *
     * @param b 注册事件的按钮
     * @param type 类型
     *
     */
    public void showShape(JButton b, int type){
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setType(type);
            }
        });
    }
    /**
     *注册事件
     *
     * @param b 注册事件的按钮
     *
     */
    public void setRotate(JButton b){
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isRotate){
                    isRotate = false;
                    b.setText("暂停");
                    rtri = 0f;
                }
                else {
                    isRotate = true;
                    b.setText("旋转");
                }

            }
        });
    }
    public void setMove(JButton b){
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isMove){
                    isMove = false;
                    b.setText("暂停");
                    move = 0f;
                }
                else {
                    isMove = true;
                    b.setText("平移");
                }

            }
        });
    }
    public void setScale(JButton b){
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isScale){
                    isScale = false;
                    b.setText("暂停");
                }
                else {
                    isScale = true;
                    b.setText("缩放");
                }

            }
        });
    }

    private void Line(GL2 gl2){
        gl2.glBegin (GL2.GL_LINES);//static field
        gl2.glVertex3f(0.50f+move,-0.50f,0);
        gl2.glVertex3f(-0.50f+move,0.50f,0);
        gl2.glEnd();
    }
    private void Triangle(GL2 gl2){
        gl2.glBegin(GL2.GL_TRIANGLES);
        gl2.glVertex3f( 0.0f+move, 0.5f, 0.0f);
        gl2.glVertex3f(-0.5f+move,-0.5f, 0.0f);
        gl2.glVertex3f( 0.5f+move,-0.5f, 0.0f);
        gl2.glEnd();
    }
    private void Quad(GL2 gl2){
        gl2.glBegin(GL2.GL_QUADS);
        gl2.glVertex3f(-0.5f+move, 0.5f, 0.0f);				// Top Left
        gl2.glVertex3f( 0.5f+move, 0.5f, 0.0f);				// Top Right
        gl2.glVertex3f( 0.5f+move,-0.5f, 0.0f);				// Bottom Right
        gl2.glVertex3f(-0.5f+move,-0.5f, 0.0f);				// Bottom Left
        gl2.glEnd();
    }

}
